package com.example.acer.startproject_123lz118.model;

import com.example.acer.startproject_123lz118.bean.WeatherResult;
import com.example.acer.startproject_123lz118.iface.Weatheriface;
import com.example.acer.startproject_123lz118.iface.WeatherListener;
import com.example.acer.startproject_123lz118.service.WeatherService;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by acer on 2017/3/21.
 */

public class Weather51Model implements  Weatheriface {
    private Retrofit retrofit;
    private String BASEURL="http://weather.51wnl.com/weatherinfo/";
    private WeatherService weatherService;
    public Weather51Model(){
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    //Retrofit使用3-2
    public void getWeather(String cityno, final WeatherListener listener) {
    weatherService=retrofit.create(WeatherService.class);
    Call<WeatherResult> call=weatherService.getResult(cityno);
    call.enqueue(new Callback<WeatherResult>() {
        @Override
        public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
            if(response.isSuccessful()&& response.body()!=null)
                listener.onResponse(response.body().getWeatherinfo());
            else
                listener.onFail("onresponse fail");
        }

        @Override
        public void onFailure(Call<WeatherResult> call, Throwable t) {
            listener.onFail(t.toString());
        }
    });
}
}
